package com.qf.portal.service.impl;

import com.qf.common.jedis.JedisClient;
import com.qf.common.util.JsonUtils;
import com.qf.common.util.StrKit;
import com.qf.portal.dao.BannerMapper;
import com.qf.portal.pojo.po.Banner;
import com.qf.portal.pojo.po.BannerExample;
import com.qf.portal.pojo.po.Drugcat;
import com.qf.portal.service.PortalBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * Created by Administrator on 2018/6/3.
 */
@Service
public class PortalBannerServiceImpl implements PortalBannerService {

    @Autowired
    private BannerMapper mapper;
    @Autowired
    private JedisClient jedisClient;
    @Override
    public List<Banner> listBannerByCid(Integer cid) {
        try{
            String json = jedisClient.hget("bannerlist", cid + "");
            if(StrKit.notBlank(json)){
                return JsonUtils.jsonToList(json,Banner.class);
            }
        }catch (Exception e){e.printStackTrace();}

        BannerExample example = new BannerExample();
        BannerExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryidEqualTo(cid);
        List<Banner> banners = mapper.selectByExample(example);

        try{
            jedisClient.hset("bannerlist",cid + "",JsonUtils.objectToJson(banners));
        }catch (Exception e){e.printStackTrace();}
        return banners;
    }

    @Override
    public List<Drugcat> listDrugcat() {
        try{
            String json = jedisClient.get("drugcatlist");
            if(StrKit.notBlank(json)){
                return JsonUtils.jsonToList(json,Drugcat.class);
            }
        }catch (Exception e){e.printStackTrace();}

        List<Drugcat> drugcatList = mapper.listDrugcat();

        try {
            jedisClient.set("drugcatList", JsonUtils.objectToJson(drugcatList));
        }catch (Exception e){e.printStackTrace();}

        return drugcatList;
    }
}
